﻿Imports System.Xml
Imports System.IO
Module modMain
    Public diceQty As Integer
    Public diceType As String
    Public sides As Integer
    Public Sub numSides()
        'Gets the type of Dice
        diceType = frmMain.cbDiceType.Text

        'Declares variables for use as dice type reference
        Dim doc As XmlDocument
        Dim nodelist As XmlNodeList
        Dim node As XmlNode

        'Create XML Document
        doc = New XmlDocument()

        'Load XML Document
        doc.Load("Dice Bag.xml")

        'Gets a list of the nodes
        nodelist = doc.SelectNodes("/DiceBag/DiceInfo")

        'Set the number of sides based on Dice Type
        For Each node In nodelist
            If diceType = node.Attributes.GetNamedItem("Name").Value Then
                sides = node.FirstChild.InnerText
            End If
        Next
    End Sub
    Public Sub rollDice()
        'Declares variables for use with rolling the Dice
        Dim intRoll As Integer
        Dim final As Integer

        'Declares and assigns a value to the Modifier Variable
        Dim modDice As Integer
        If frmMain.tbRollMod.Text = vbNullString Then
            modDice = CInt(0)
        Else
            modDice = CInt(frmMain.tbRollMod.Text)
        End If

        'Clears the List View of Rolls
        frmMain.lvRollList.Items.Clear()

        If diceQty = 1 Then
            'Rolls the Dice
            intRoll = CInt(Int(Rnd() * (sides + 1)))

            'Ensures that the Roll is between 1 and the number of the sides on the dice
            If intRoll > sides Then
                intRoll = sides
            ElseIf intRoll < 1 Then
                intRoll = 1
            End If

            'Displays the Roll in the ListView Container
            frmMain.lvRollList.Items.Add("Roll 1")
            frmMain.lvRollList.Items(0).SubItems.Add(CStr(intRoll))

            'Adds the Final Modifier
            final = intRoll + modDice

            'Displays the Final Value in MsgBox
            'MsgBox(CStr(final), MsgBoxStyle.OkOnly)

            'Assigns the value to the Textbox
            frmMain.tbRollTotal.Text = CStr(final)

        Else

            'Declares index for Loop
            Dim roll As Integer
            Dim iarry(diceQty) As Integer
            Dim read As Integer

            'Loops to roll the selected amount of Dice
            For roll = 0 To (diceQty - 1) Step 1

                'Rolls the Dice
                intRoll = CInt(Int(Rnd() * (sides + 1)))

                'Ensures that the Roll is between 1 and the number of the sides on the dice
                If intRoll > sides Then
                    intRoll = sides
                ElseIf intRoll < 1 Then
                    intRoll = 1
                End If
                iarry(roll) = intRoll
            Next

            'Displays the rolls in the Roll List
            For read = 0 To (diceQty - 1) Step 1
                frmMain.lvRollList.Items.Add("Roll " & read + 1)
                frmMain.lvRollList.Items(read).SubItems.Add(CStr(iarry(read)))
            Next

            'Adds the rolls together to get the roll total
            Dim element As Integer

            For Each element In iarry
                final = final + element
            Next

            'Adds the Roll Modifer to the Total
            final = final + modDice


            'Displays final total
            'MsgBox(CStr(final), MsgBoxStyle.OkOnly)

            'displays total in textbox
            frmMain.tbRollTotal.Text = CStr(final)

        End If
    End Sub
End Module
